package com.kamistoat.meimeimall.meimeimallseckill.web;

import com.kamistoat.meimeimall.common.To.seckillTo.SecKillProductTo;
import com.kamistoat.meimeimall.common.To.seckillTo.SecKillSessionTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.exception.BizCodeEnum;
import com.kamistoat.meimeimall.common.exception.RRException;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallseckill.entity.SeckillSessionEntity;
import com.kamistoat.meimeimall.meimeimallseckill.service.SeckillSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "秒杀活动管理页面")
@RestController
@RequestMapping("web")
public class SecKillManagePageController {

    @Autowired
    SeckillSessionService seckillSessionService;

    @ApiOperation("强制删除缓存中所有的秒杀信息")
    @GetMapping("/clear_all_seckill")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R clearAllSecKillSession() {
        seckillSessionService.clearAllSecKillSession();
        return R.ok();
    }

    @ApiOperation("将未来3天内的秒杀活动预热到缓存中")
    @GetMapping("/upload_three_day_seckill")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R uploadThreeDaySecKill() {
        seckillSessionService.uploadThreeDaySecKill();
        return R.ok();
    }

    @ApiOperation("从数据库查询所有未来秒杀活动(不限三天内)")
    @GetMapping("/get_all_seckill_from_database")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R getAllSecKillFromDatabase() {
        List<SecKillSessionTo> sessionToList = seckillSessionService.getAllSecKillSessionFormDatabase();
        return R.ok().put("sessionToList", sessionToList);
    }

    @ApiOperation("强制中止一个或多个秒杀活动")
    @PostMapping("/terminate_seckill_session")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R terminateSecKillSession(@RequestBody List<String> sessionKeys) {
        seckillSessionService.terminateSessions(sessionKeys);
        return R.ok();
    }

    @ApiOperation("提交新的秒杀活动本体")
    @PutMapping("/manage_seckill_session")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R addSecKillSession(@RequestBody SeckillSessionEntity sessionEntity) {
        seckillSessionService.addSecKillSession(sessionEntity);
        return R.ok();
    }

    @ApiOperation("修改已有的秒杀活动本体。不允许修改正在进行的秒杀活动。如果修改对象是三天内，会刷新其预热")
    @PostMapping("/manage_seckill_session")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R updateSecKillSession(@RequestBody SeckillSessionEntity sessionEntity) {
        seckillSessionService.updateSecKillSession(sessionEntity);
        return R.ok();
    }

    @ApiOperation("为秒杀活动添加秒杀商品")
    @PutMapping("/manage_session_product/{sessionId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R boundSessionProduct(@PathVariable("sessionId") Long sessionId, @RequestBody List<SecKillProductTo> secKillProductToList) {
        if(secKillProductToList == null || secKillProductToList.size() == 0) {
            return R.ok();
        }
        List<SecKillProductTo> isValid = secKillProductToList.stream().filter(secKillProductTo -> {
            return !secKillProductTo.getSessionId().equals(sessionId);
        }).collect(Collectors.toList());
        if(isValid.size() > 0) {
            throw new RRException(BizCodeEnum.PRODUCT_NOT_SAME_SESSION.getMessage(), BizCodeEnum.PRODUCT_NOT_SAME_SESSION.getCode());
        }
        seckillSessionService.boundSessionProduct(secKillProductToList);
        return R.ok();
    }

    @ApiOperation("为秒杀活动中修改秒杀商品")
    @PostMapping("/manage_session_product/{sessionId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R updateSessionProduct(@PathVariable("sessionId") Long sessionId, @RequestBody List<SecKillProductTo> secKillProductToList) {
        if(secKillProductToList == null || secKillProductToList.size() == 0) {
            return R.ok();
        }
        List<SecKillProductTo> isValid = secKillProductToList.stream().filter(secKillProductTo -> {
            return !secKillProductTo.getSessionId().equals(sessionId);
        }).collect(Collectors.toList());
        if(isValid.size() > 0) {
            throw new RRException(BizCodeEnum.PRODUCT_NOT_SAME_SESSION.getMessage(), BizCodeEnum.PRODUCT_NOT_SAME_SESSION.getCode());
        }
        seckillSessionService.updateSessionProduct(secKillProductToList);
        return R.ok();
    }

    @ApiOperation("从秒杀活动中删除秒杀商品")
    @DeleteMapping("/manage_session_product/{sessionId}")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    public R deleteSessionProduct(@PathVariable("sessionId") Long sessionId, @RequestBody List<Long> secKillProductToIds) {
        return R.ok();
    }
}
